<?
/**
 * 
 **/
class helper_memcached
{
    static $memcache;
        
    private function init( $host, $port ) { 
        if ( helper_memcached::$memcache != null ) { 
            return;
        }   
            
        helper_memcached::$memcache = new Memcache;
        helper_memcached::$memcache->connect($host, $port) or die ("Could not connect");
    }   
        
    public function __construct( $host="localhost", $port=11212 ) { 
        $this->init( $host, $port );
    }   
        
    public function __destruct() {
        if ( ! helper_memcached::$memcache ) { 
            helper_memcached::$memcache->close();
            helper_memcached::$memcache = null;
        }   
    }   
        
    public function set( $key, $value, $time = 10) {
        $compress = is_bool($value) || is_int($value) || is_float($value) ? false : MEMCACHE_COMPRESSED;
        $result = helper_memcached::$memcache->set($key, $value, $compress, $time) or die ("Failed to save data at the server");
    }   
        
    public function get( $key ) { 
        return helper_memcached::$memcache->get($key);
    }   
}

